<?php
/**
* @author trungdt
* @since Sep 12, 2011 
* Change logs
* 
* 
*/
if (!defined("EXPONENT")) exit("");
	$article = null;
	if (isset($_GET['id'])) {
		$article = $db->selectObject("article","id=".intval($_GET['id']));
		if ($article != null) {
			$loc = unserialize($article->location_data);
		} else {
			echo SITE_404_HTML;
		}
	}
	
	if (!$article) {
		$article->category_id = 0;
	}
	
	if (exponent_permissions_check("manage",$loc) 
		|| exponent_permissions_check("create",$loc)) {
		$config = $db->selectObject('articlemodule_config',"location_data='".serialize($loc)."'");
		if ($config == null) {
			$config->enable_categories = 0;
			$object->allow_submissions = 0;
			$object->require_login = 0;
			$object->item_per_page = 10;
			// Sep 12, 2011 - trungdt - aggregate - show on multi module instance
			$object->aggregate = array();
		}
		$form = article::form($article);
		$form->location($loc);
		$form->meta("action","save_article");
				
		$template = new template("articlemodule","_form_editarticle",$loc);
		if ($config->enable_categories) {
			// Sep 12, 2011 - trungdt - Change the way we get all cats - begin
			$locsql = "(location_data='".serialize($loc)."'";
			if (!empty($config->aggregate)) {
				$locations = unserialize($config->aggregate);
				foreach ($locations as $source) {
					$tmploc = null;
					$tmploc->mod = $loc->mod;
					$tmploc->src = $source;
					$tmploc->int = '';
					$locsql .= " OR location_data='".serialize($tmploc)."'";
				}
			}
			$locsql .= ')';

			$allcats = $db->selectObjects('category', "{$locsql}");
			$articles = exponent_modules_getModuleInstancesByType('articlemodule');
			
			$catarray = array();
			foreach ($allcats as $cat) {
				$temp=unserialize($cat->location_data);
				foreach ($articles as $k1=>$article_mod){
					if ($k1 == $temp->src)
					{
						$cat->name=$article_mod[0]->title." :: ". $cat->name;
						break;
					}
				}
				$catarray[$cat->id] = $cat->name;
			}
			
			// Sep 12, 2011 - trungdt - Change the way we get all cats - end
			$form->registerBefore('title', 'categories', 'Select Category', new dropdowncontrol($article->category_id, $catarray));
		}
		$template->assign("is_edit",(isset($_GET['id']) ? 1 : 0));
		$template->assign("form_html",$form->toHTML());
		$template->output();
	} else {
		echo SITE_403_HTML;
	}
	
?>